import 'package:flutter_module/common/base/BaseTabSwitch/base_tab_switch_view.dart';
import 'package:flutter_module/common/utils/aliyunImage.dart';
import 'package:flutter_module/common/widgets/LoadStateWidget.dart';
import 'package:flutter_module/res/colors.dart';
import 'package:flutter/material.dart';
import 'package:flutter_boost/flutter_boost.dart';
import 'package:flutter_svg/svg.dart';
import 'package:get/get.dart';
import '../../../common/base/page/BasePage.dart';
import '../../../common/base/page/BasicListPage.dart';
import '../../../model/orgainize.dart';
import '../../../res/image.dart';
import 'search_colleague_logic.dart';
import 'search_colleague_state.dart';

/// @description: 点赞列表同事页
/// @author
/// @date: 2022-12-30 15:23:31
class SearchColleaguePage extends BasePage {
  final SearchColleagueLogic logic = Get.put(SearchColleagueLogic());
  final SearchColleagueState state = Get.find<SearchColleagueLogic>().state;

  bool isSelectAgain = false;

  /// 首要部门Id
  int orgSignificantDepartmentId = 0;

  SearchColleaguePage({Key? key, required this.orgSignificantDepartmentId})
      : super(key: key) {
    logic.state.orgSignificantDepartmentId = orgSignificantDepartmentId;
  }

  @override
  State<StatefulWidget> createState() => _SearchColleaguePageState();
}

class _SearchColleaguePageState
    extends BasicListPage<SearchColleaguePage, SearchColleagueLogic> {
  // 状态栏高度
  double statusBarHeight = 0;

  @override
  SearchColleagueLogic get currentLogic => widget.logic;

  @override
  Widget listItem(int i) {
    return _cell(currentLogic.dataList[i]);
  }

  @override
  Widget body(BuildContext context) {
    // 使用MediaQuery获取状态栏高度
    statusBarHeight = MediaQuery.of(context).padding.top;
    return super.body(context);
  }

  @override
  Color scaffoldBackgroundColor() {
    return YouthColors.color_F1F3F7;
  }

  @override
  EmptyType? emptyType() {
    if (currentLogic.state.pageType == 0) {
      return EmptyType.Empty_histroyLikeSerch;
    } else {
      return EmptyType.Empty_Search;
    }
  }

  // @override
  // EmptyType? emptyType() {
  //   return EmptyType.Empty_Welfare_Ranking;
  // }

  @override
  Widget? header() {
    return _header();
  }

  @override
  Widget? listHeaderView() {
    return _switchView();
  }

  @override
  String screenName() {
    return "";
  }

  Container _header() {
    return Container(
        child: Column(
      mainAxisAlignment: MainAxisAlignment.start,
      children: [
        Container(
            child: Row(
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            SvgPicture.asset(
              YouthImages.icSearchOrange,
              width: 32,
              height: 32,
            ),
            const SizedBox(
              width: 5,
            ),
            Expanded(
                child: GetBuilder<SearchColleagueLogic>(
              init: SearchColleagueLogic(),
              builder: (_) {
                return TextField(
                  controller: currentLogic.state.controller,
                  decoration: InputDecoration(
                    // contentPadding: const EdgeInsets.only(top: -2),
                    hintText: '搜索需要点赞的同事',
                    border: InputBorder.none,
                    suffixIcon: Visibility(
                      visible: currentLogic.state.isShowClearBtn,
                      child: FloatingActionButton(
                        onPressed: () => currentLogic.clearSearchText(),
                        backgroundColor: Colors.white,
                        elevation: 0,
                        child: const Image(
                            image: AssetImage(YouthImages.icInputDelete)),
                      ),
                    ),
                  ),
                  focusNode: currentLogic.state.userFocusNode,
                  onChanged: (value) => currentLogic.completeInput(value),
                  onSubmitted: (value) => currentLogic.completeInput(value),
                );
              },
            )),
            const SizedBox(
              width: 10,
            ),
            FloatingActionButton(
              backgroundColor: Colors.white,
              elevation: 0,
              onPressed: () => BoostNavigator.instance.pop(),
              child: const Text('取消',
                  style: TextStyle(fontSize: 17, color: Color(0xFF453838))),
            ),
            const SizedBox(
              width: 5,
            ),
          ],
        )),
        // const Row(
        //   children: [Expanded(child: SizedBox(height: 10))],
        // ),
        const SizedBox(
          height: 6,
        ),
        // const Row(
        //   children: [Expanded(child: SizedBox(height: 6))],
        // ),
      ],
    ));
  }

  Widget _cell(Orgainize user) {
    return InkWell(
      onTap: () => currentLogic.selectBtnClicked(user),
      child: Container(
        padding: EdgeInsets.only(left: 16, right: 16),
        constraints: BoxConstraints(minHeight: 63),
        color: Colors.white,
        child: Row(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            _logo(user),
            textContainer(user),
            _selectedBtn(user),
          ],
        ),
      ),
    );
  }

  /// 切换器
  Widget _switchView() {
    List<BaseTabModel> modelList = [];
    modelList.add(BaseTabModel(labelId: 0, labelName: "历史点赞"));
    modelList.add(BaseTabModel(labelId: 1, labelName: "部门同事"));
    modelList.add(BaseTabModel(labelId: 2, labelName: "全部"));
    return BaseTabSwitchView(
        switchTabModels: modelList,
        onChangeStatus: (model) {
          // 切换列表，刷新请求
          currentLogic.state.pageType = model.labelId;
          currentLogic.page = 1;
          currentLogic.requestUserList(
              true, currentLogic.configNetWorkParmas());
        });
  }

  Widget _selectedBtn(Orgainize user) {
    return Column(
      mainAxisAlignment: MainAxisAlignment.start,
      children: [
        const SizedBox(
          height: 17,
        ),
        Container(
          height: 30,
          width: 70,
          child: FloatingActionButton(
            backgroundColor: const Color(0xFFFFECE3),
            shape: const RoundedRectangleBorder(
                borderRadius: BorderRadius.all(Radius.circular(15))),
            elevation: 0,
            onPressed: () => currentLogic.selectBtnClicked(user),
            child: const Text('选择',
                style:
                    TextStyle(fontSize: 14, color: YouthColors.colorPrimary)),
          ),
        ),
      ],
    );
  }

  Widget textContainer(Orgainize user) {
    return Expanded(
      child: Padding(
        padding: const EdgeInsets.only(left: 10, right: 10),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            const SizedBox(
              height: 12,
            ),
            Text(
              user.userName ?? '',
              overflow: TextOverflow.ellipsis,
              style: const TextStyle(
                  fontSize: 15, color: YouthColors.colorContent),
            ),
            const SizedBox(
              height: 2,
            ),
            Text(
              user.departmentInfo ?? '',
              softWrap: true,
              maxLines: 2,
              overflow: TextOverflow.ellipsis,
              style: const TextStyle(fontSize: 12, color: Color(0x66000000)),
            ),
            const SizedBox(
              height: 6,
            ),
            // _divider(),
          ],
        ),
      ),
    );
  }

  Widget _logo(Orgainize user) {
    return Padding(
      padding: const EdgeInsets.only(top: 10),
      child: Column(
        mainAxisAlignment: MainAxisAlignment.start,
        children: [
          ClipRRect(
            borderRadius: BorderRadius.circular(20),
            child: FadeInImage.assetNetwork(
              placeholder: YouthImages.icDefaultHeader,
              image: YZAliyunImage.bmiddleImage(user.userLogo,
                  placeholder: YouthImages.defaultHeaderUrl),
              width: 40,
              height: 40,
              fit: BoxFit.cover,
              // placeholderCacheWidth: 40,
              // placeholderCacheHeight: 40,
            ),
          ),
        ],
      ),
    );
  }

  /// 分割线
  // Divider _divider() {
  //   return const Divider(
  //     height: 0.5,
  //     // Divider 组件高度
  //     thickness: 0.5,
  //     // 分割线线宽，分割线居于 Divider 中心
  //     indent: 0,
  //     // 分割线左侧间距(间距+头像+间距)
  //     endIndent: 0,
  //     // 分割线右侧间距
  //     color: YouthColors.color_EDEBEB, // 分割线颜色
  //   );
  // }
}
